package com.jd.controller;

import com.jd.domain.User;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Autowired
    private RestTemplate restTemplate;

    @GetMapping("/getOrderById/{id}")
    @HystrixCommand(fallbackMethod = "getOrderByIdFallback")
    public User getOrderById(@PathVariable("id") Long id){
        // spring给我吗提供了
        // String url = "http://localhost:1020/user/getUserById/"+id;
        String url = "http://user-service/user/getUserById/"+id;
        User user = restTemplate.getForObject(url, User.class);
        return user;
    }

    // 响应托底数据方法
    public User getOrderByIdFallback(Long id){
        return new User(-1L, "服务熔断触发", "用户服务不可用！");
    }
}